package com.jty.lib_base.data.source

import com.blankj.utilcode.util.CacheDiskUtils
import com.blankj.utilcode.util.GsonUtils
import com.czl.lib_base.config.AppConstants
import com.google.gson.reflect.TypeToken
import com.jty.lib_base.data.bean.*
import com.jty.lib_base.utils.SpHelper
import java.io.Serializable


/**
 * author: zhouyh
 * created on: 2021/5/11 11:22 下午
 * description:本地数据源实现类  缓存实现类
 */
class LocalDataImpl : LocalDataSource {


    override fun getLoginName(): String? {
        return SpHelper.decodeString(AppConstants.SpKey.LOGIN_NAME)
    }

    override fun getLoginPassword(): String? {
        return SpHelper.decodeString(AppConstants.SpKey.LOGIN_PWD)
    }

    override fun saveAccountAndPwd(account: String, password: String) {
        SpHelper.encode(AppConstants.SpKey.LOGIN_NAME,account)
        SpHelper.encode(AppConstants.SpKey.LOGIN_PWD,password)
    }

    override fun savePenMac(penMac: String) {
        SpHelper.encode(AppConstants.SpKey.PEN_MAC,penMac)
    }
    override fun getPenMac() : String?{
        return SpHelper.decodeString(AppConstants.SpKey.PEN_MAC)
    }



    override fun saveUserInfoData(userBean: UserInfoBean) {
        SpHelper.encode(AppConstants.SpKey.USER_JSON_DATA,
            GsonUtils.toJson(userBean, object : TypeToken<UserInfoBean>() {}.type))

    }

    override fun getUserId(): Int? {
        return SpHelper.decodeInt(AppConstants.SpKey.USER_ID)
    }

    override fun getUserName(): String? {
        return SpHelper.decodeString(AppConstants.SpKey.USER_NAME)
    }

    override fun saveLoginState(isLogin: Boolean) {
        SpHelper.encode(AppConstants.SpKey.USER_LOGIN_STATE,isLogin)
    }

    override fun isLogin(): Boolean {
        return SpHelper.decodeBoolean(AppConstants.SpKey.USER_LOGIN_STATE)
    }




    /**
     * 获取本地Token信息
     */
    override fun getToken(): String? {
        return SpHelper.decodeString(AppConstants.SpKey.USER_TOKEN)
    }


    /**
     * 保存Token信息
     */
    override fun saveLoginData(loginBean: LoginBean) {
        SpHelper.encode(AppConstants.SpKey.USER_NAME, loginBean.name)
        SpHelper.encode(AppConstants.SpKey.USER_ID, loginBean.id)
        SpHelper.encode(AppConstants.SpKey.USER_TOKEN, loginBean.token)
        SpHelper.encode(AppConstants.SpKey.TOKEN_JSON_DATA,
            GsonUtils.toJson(loginBean, object : TypeToken<LoginBean>() {}.type))
    }

    override fun getLoginData(): LoginBean? {
        val tokenJsonData = SpHelper.decodeString(AppConstants.SpKey.TOKEN_JSON_DATA)
        return if (tokenJsonData.isBlank())
            return null
        else GsonUtils.fromJson(
            tokenJsonData,
            object : TypeToken<LoginBean>(){}.type
        )
    }

    override fun getUserInfoData(): UserInfoBean? {
        val userJsonData = SpHelper.decodeString(AppConstants.SpKey.USER_JSON_DATA)
        return if (userJsonData.isBlank())
            return null
        else GsonUtils.fromJson(
            userJsonData,
            object : TypeToken<UserInfoBean>() {}.type
        )
    }

    override fun <T : Serializable> saveCacheListData(list: List<T>) {
        if (list.isNotEmpty()) {
            when (list[0]) {
                is ResearchActivityListBean.Record->{
                    CacheDiskUtils.getInstance().put(
                        AppConstants.CacheKey.CACHE_RESEARCH_CONTENT,
                        ResearchBeanCache(list as List<ResearchActivityListBean.Record>),
                        AppConstants.CacheKey.CACHE_SAVE_TIME_SECONDS
                    )
                }
                is PortraitBean ->{
                    CacheDiskUtils.getInstance().put(
                        AppConstants.CacheKey.CACHE_PORTRAIT_DATA,
                        PortraitBeanCache(list as List<PortraitBean>),
                        AppConstants.CacheKey.CACHE_SAVE_TIME_SECONDS
                    )
                }
                is ActivityTreeListBean->{
                    CacheDiskUtils.getInstance().put(
                        AppConstants.CacheKey.CACHE_RECORD_DATA,
                        ActivityTreeListBeanCache(list as List<ActivityTreeListBean>),
                        AppConstants.CacheKey.CACHE_SAVE_TIME_SECONDS
                    )
                }

                is ResearchTabBean ->{
                    CacheDiskUtils.getInstance().put(
                        AppConstants.CacheKey.CACHE_RESEARCH_TAB_DATA,
                        ResearchTabBeanCache(list as List<ResearchTabBean>),
                        AppConstants.CacheKey.CACHE_SAVE_TIME_SECONDS
                    )
                }

            }

        }
    }


    override fun <T : Serializable> getCacheListData(key: String): List<T>? {
        return when (val serializable = CacheDiskUtils.getInstance().getSerializable(key)) {
            is ResearchBeanCache? ->{
                serializable?.researchBeanCaches as List<T>
            }
            is PortraitBeanCache?->{
                serializable?.portraitBeanCaches as List<T>
            }
            is ActivityTreeListBeanCache?->{
                serializable?.ActivityTreeListBeanCaches as List<T>
            }
            is ResearchTabBeanCache?->{
                serializable?.ResearchTabBeanCaches as List<T>
            }
            else -> emptyList()

        }
    }

    override fun saveCurrentStudyYearData(studyYearBean: CurrentStudyYearBean) {
        SpHelper.encode(AppConstants.SpKey.CURRENT_STUDY_YEAR_DATA,
            GsonUtils.toJson(studyYearBean, object : TypeToken<CurrentStudyYearBean>() {}.type))
    }

    override fun getCurrentStudyYearData(): CurrentStudyYearBean? {
        val studyYearBean = SpHelper.decodeString(AppConstants.SpKey.CURRENT_STUDY_YEAR_DATA)
        return if (studyYearBean.isBlank())
            return null
        else GsonUtils.fromJson(
            studyYearBean,
            object : TypeToken<CurrentStudyYearBean>() {}.type
        )
    }

    override fun savePwdFlag(isVisible: Boolean) {
        SpHelper.encode(AppConstants.SpKey.SAVE_PWD,isVisible)
    }

    override fun getPwdFlag(): Boolean {
        return SpHelper.decodeBoolean(AppConstants.SpKey.SAVE_PWD)
    }

    override fun outLoginReset() {
        SpHelper.encode(AppConstants.SpKey.USER_JSON_DATA, "")
        SpHelper.encode(AppConstants.SpKey.TOKEN_JSON_DATA, "")
        SpHelper.encode(AppConstants.SpKey.HEAD_AUTHORIZATION, "")
        SpHelper.encode(AppConstants.SpKey.USER_TOKEN, "")
        SpHelper.encode(AppConstants.SpKey.TEACHER_ID, "")
        SpHelper.encode(AppConstants.SpKey.PEN_MAC, "")
    }
}